package com.yumeko.controller;

import com.yumeko.pojo.Resume;
import com.yumeko.service.ResumeService;
import com.yumeko.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @program: ssp
 * @description:
 * @author: Zheng Hao
 * @create: 2020-01-14 23:39
 */
@Controller
public class ResumeController {

    @Autowired
    private ResumeService resumeService;
    //测试用
    @RequestMapping(value = "/test")
    public String test(){
        return "test";
    }
    //默认登陆页
    @RequestMapping(value = "/")
    public String login(){
        return "login";
    }
    //登陆
    @RequestMapping(value = "/toLogin")
    public String toLogin(@RequestParam("userName") String userName, @RequestParam("password") String password, HttpSession session, Model model) {
        Boolean aBoolean = resumeService.doLogin(userName, password, session);
        if (userName!=null&&password!=null){
            if (aBoolean){
                return "resumeList";
            }else{
                model.addAttribute("loginFlag","登陆失败");
                return "redirect:/";
            }
        }else{
            model.addAttribute("loginFlag","登陆失败");
            return "";
        }
    }
    //分页排序查询
    @RequestMapping("/doFindResume")
    @ResponseBody
    public ResponseData doFindResume(String page,String limit) {
        Page<Resume> resumes = resumeService.doFindResume(page, limit);
        return new ResponseData<>((int)resumes.getTotalElements() ,resumes.getContent());
    }
    //添加
    @RequestMapping("/doInsetResume")
    @ResponseBody
    public Integer doInsetResume(String userName, String password) {
        return resumeService.doInsetResume(userName, password);
    }
    //更新
    @RequestMapping("/doUpdateResume")
    @ResponseBody
    public Integer doUpdateResume(Integer userId, String userName, String password) {
        return resumeService.doUpdateResume(userId, userName, password);
    }
    //删除
    @RequestMapping("/doDeleteResume")
    @ResponseBody
    public void doDeleteResume(Integer userId) {
        resumeService.doDeleteResume(userId);
    }
}
